package cn.innosmart.aq.adapter;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.widget.TextViewCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.LvMenuItem;
import cn.innosmart.aq.util.SystemConstant;

/**
 * 进入Box后的左划菜单适配器
 * Created by David on 2015/7/27.
 */
public class BoxDetailMenuItemAdapter extends BaseAdapter {
    private final int mIconSize;
    private LayoutInflater mInflater;
    private Context mContext;
    private List<LvMenuItem> mItems;
    private View.OnClickListener onClickListener;
    private int index = 1;

    public BoxDetailMenuItemAdapter(Context context, List<LvMenuItem> list) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        mIconSize = AutoUtils.getPercentWidthSize(70);//24dp
        mItems = list;
    }

    /**
     * 设置菜单
     *
     * @param list
     */
    private void setMenu(ArrayList list) {
        mItems = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 左划菜单的三类
     * 无图标、有图标、分割线
     *
     * @return
     */
    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LvMenuItem item = mItems.get(position);
        switch (item.type) {
            case LvMenuItem.TYPE_NORMAL:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.design_drawer_item, parent,
                            false);
                }
                TextView itemView = (TextView) convertView.findViewById(R.id.tv_title);
                itemView.setText(item.name);
                Drawable icon = mContext.getResources().getDrawable(item.icon);
                if (position == index) {
                    convertView.setBackgroundColor(mContext.getResources().getColor(R.color.light_grey));
                    itemView.setTextColor(mContext.getResources().getColor(R.color.font_blue));
                } else {
                    convertView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
                    itemView.setTextColor(mContext.getResources().getColor(R.color.menu_font_style));
                }
                if (icon != null) {
                    icon.setBounds(0, 0, mIconSize, mIconSize);
                    TextViewCompat.setCompoundDrawablesRelative(itemView, icon, null, null, null);
                }
                itemView.setTag(position);
                itemView.setOnClickListener(onClickListener);
                break;
            case LvMenuItem.TYPE_NO_ICON:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.design_drawer_item_subheader,
                            parent, false);
                }
                TextView subHeader = (TextView) convertView.findViewById(R.id.tv_title);
                subHeader.setText(item.name);
                break;
            case LvMenuItem.TYPE_SEPARATOR:
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.design_drawer_item_separator,
                            parent, false);
                }
                break;
        }

        return convertView;
    }

    /**
     * 设置菜单的Icon图标颜色
     *
     * @param icon
     */
    public void setIconColor(Drawable icon) {
        int textColorSecondary = android.R.attr.textColorSecondary;
        TypedValue value = new TypedValue();
        if (!mContext.getTheme().resolveAttribute(textColorSecondary, value, true)) {
            return;
        }
        int baseColor = mContext.getResources().getColor(value.resourceId);
        icon.setColorFilter(baseColor, PorterDuff.Mode.MULTIPLY);
    }

    /**
     * 动态创建鱼缸左划菜单
     */
    public void creatAquariumMenu() {
        mItems.clear();
        mItems.add(new LvMenuItem(R.drawable.aquarium_left_menu, mContext.getString(R.string.boxdetail_leftmenu_all_aquarium)));
        if (SystemConstant.aquariumList != null) {
            for (int i = 0; i < SystemConstant.aquariumList.size(); i++) {
                mItems.add(new LvMenuItem(R.drawable.aquarium_left_menu, SystemConstant.aquariumList.get(i).getTitle()));
            }
        }
        mItems.add(new LvMenuItem());
        mItems.add(new LvMenuItem(mContext.getString(R.string.boxdetail_leftmenu_function)));
        mItems.add(new LvMenuItem(R.drawable.left_menu_share_icon, mContext.getString(R.string.boxdetail_leftmenu_notification)));
        mItems.add(new LvMenuItem(R.drawable.left_menu_share_icon, mContext.getString(R.string.boxdetail_leftmenu_share)));
        mItems.add(new LvMenuItem(R.drawable.left_menu_share_icon, mContext.getString(R.string.boxdetail_leftmenu_control_center)));
        notifyDataSetChanged();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    /**
     * 设置菜单高亮的位置
     *
     * @param position
     */
    public void setHighLightIndex(int position) {
        index = position;
        notifyDataSetChanged();
    }
}